package com.mySampleApplication.client.codeRule;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.mySampleApplication.client.components.SDataSource;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.i18n.SmartGwtMessages;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CodeRuleLayout extends VLayout {
    public CodeRuleLayout() {
        // 创建一个垂直布局容器
        VLayout layout = new VLayout(10); // 参数15为布局成员之间的间距
        layout.setWidth100();
        layout.setHeight100();
        layout.setPadding(10);
        DataSource dataSource = SDataSource.getDataSource();
        Map<String, Object> listMap = CodeRule.getListGrid();
        ListGrid listGrid = (ListGrid) listMap.get("listGrid");
        List<ListGridRecord> recordList = new ArrayList<>();
        Map<String, String> mapPage = new HashMap<>();
        mapPage.put("pageSize", "1");
        mapPage.put("pageNumber", "10");
        CodeRuleService.App.getInstance().getSelect(new HashMap<>(), mapPage, new AsyncCallback<CodeRuleDto>() {
            public void onSuccess(CodeRuleDto result) {
                List<Map<String, String>> keyValueMap = result.getKeyValueMap();
                for (Map<String, String> data : keyValueMap) {
                    ListGridRecord record = new ListGridRecord();
                    data.forEach((k, v) -> {
                        record.setAttribute(k, v);
                    });
                    // 设置其他需要的属性
                    recordList.add(record);

                }
                listGrid.setData(recordList.toArray(new ListGridRecord[0]));
                SC.say( "获取数据成功！");
                //list.addAll(result);
            }

            public void onFailure(Throwable caught) {
                SC.warn("获取数据失败！");
            }
        });
        // 设置数据源到列表网格
        listGrid.setDataSource(dataSource);
        // 加载数据
        listGrid.fetchData();

        DynamicForm filterForm = CodeRuleFilterForm.getFilterForm(null, listGrid);

        HLayout hLayout = CodeRuleFilterForm.getSerchButton(listGrid, filterForm);

        HLayout h = new HLayout(10); // 参数15为布局成员之间的间距
//        h.setWidth100();
//        h.setHeight100();
//        h.setPadding(10);



        h.addMembers(filterForm,hLayout);
        layout.addMember(h);
        // 组装页面
        //layout.addMember(searchArea);
       // layout.addMember((HLayout) listMap.get("buttonLayout"));
        // 添加列表网格到布局容器中
        layout.addMember(listGrid);

        // 添加布局容器到页面中
        this.addChild(layout);


    }
}
